<?
//	*****************************************
//	SbSE metadata fetch component
//	v1.0.3 / 11.09.18
//	Required:
//	lib 0.9.1
//	*****************************************

require_once E.'_lib/_const.php';

class LOADMETA {

	var $id_data;
	var $id_parent;
	var $assoc;
	var $data;
	var $varname;
	
	var $res;
	
	function LOADMETA() {

		$this->data = array();
		$this->res = 255;
	}
	
	function init($id_data, $parent, $assoc='', $varname='', $getclob = false, $source = false) {

		if (is_array($id_data))	$id_data = implode(',', $id_data);
		
		$this->id_data = $id_data;
		
		if (is_array($parent)) {
			$this->id_parent = $parent['parent'];
			$this->assoc = $parent['assoc'];
			$this->varname = $parent['varname'];
			$this->getclob = $parent['getclob'];
			$this->source = $parent['source'];
		} else {
			$this->id_parent = $parent;
			$this->assoc = $assoc;
			$this->varname = $varname;
			$this->getclob = $getclob;	
			$this->source = $source;
		}

		if (is_array($this->id_parent))	$this->id_parent = implode(',', $this->id_parent);
		
		
		$this->table = 'data';
		$this->xtable = 'xdata';
		$this->ai_field = 'id';
		$this->seq = tb('data_seq');

		$this->clob = 'value';
		$this->flag = 'large';
		$this->spacer = '<x>';
		
		
		$this->res = $this->fetch();
		return ! (bool) $this->res;
	}
	
	function fetch() {

		if ($this->varname) {
			if ($d = datafetch('db_item', tb('data'),			// get records with empty 'assoc' (i.e. for all langs) or with corresponding 'assoc'
				"(id_data IN ({$this->id_data})) AND
				(id_parent IN ({$this->id_parent})) AND
				((assoc IS NULL) OR (assoc='') OR INSTR(assoc,'{$this->assoc}')!=0) AND
				(name='{$this->varname}')" )) {			// and specified variable name

				if (isClobEmpty($d['value']))
					$d['value'] = '';
				else
				
					if (isClobFull($d['value'])) {
						if ($this->getclob) {
							 $t = datafetch('db_item', tb($this->xtable), "id=$d[id]", 'value');
							 $d['value'] = $t['value'];
						} else {
							$d['value'] = '';
						}
					}

				$this->data = $d['value'];
				return 0;
			}
				else return 1;		// no such variable

		} else {
			
			$this->data = array();

			if ($d = datafetch_array('db_list', array('table' => tb('data'), 	// get records with empty 'assoc' (i.e. for all langs) or with corresponding 'assoc'
				'where' => "(id_data IN ({$this->id_data})) AND
							(id_parent IN ({$this->id_parent})) AND
							((assoc IS NULL) OR (assoc='') OR INSTR(assoc,'{$this->assoc}')!=0)",
				'index' => 'id' ))) {


				if ($this->getclob) {

					$keys = array(); 
						foreach($d as $id => $m) 
							if (isClobFull($m[$this->clob])) $keys[] = $id; else
								if (isClobEmpty($m[$this->clob])) $d[$id][$this->clob] = '';
					
					if (sizeof($keys)) {
						$cl = datafetch_array('db_list', array(
							'table' => tb($this->xtable), 
							'where' => 'id IN ('.implode(',', $keys).')', 
							'index' => 'id'));
							
						foreach($cl as $id => $v) $d[$id][$this->clob] = $v[$this->clob];
					}

				} else 

					foreach($d as $id => $m) if (isClob($m[$this->clob])) $d[$id][$this->clob] = '';


				if ($this->source)

					$this->data = $d;

				else
	
					foreach($d as $id => $m)
						$this->data[$m['name']] = $m['value'];
			
				return 0;
			}
		}
	}
}
